package yh.thread.system.shardingtest.service.impl;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yh.thread.system.shardingtest.dao.RechargeRecordMapper;
import yh.thread.system.shardingtest.model.RechargeRecordDO;
import yh.thread.system.shardingtest.service.IRechargeRecordService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 *
 * @author: zyh
 * @date: 2024/12/13 14:49
 **/
@Service
public class RechargeRecordServiceImpl implements IRechargeRecordService {

   @Resource
   private RechargeRecordMapper rechargeRecordMapper;



    @Override
    public HashMap<String, Object> countRechargeRecord() {
       int count =  rechargeRecordMapper.countRechargeRecord();
        HashMap<String , Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("code","200");
        objectObjectHashMap.put("body",count);
        return objectObjectHashMap;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public HashMap<String, Object> saveRechargeRecord(RechargeRecordDO rechargeRecordDO) {
        int count =  rechargeRecordMapper.countRechargeRecord();
        int i = RandomUtils.nextInt();
        rechargeRecordDO.setRechargeRecordId("sharding" + i);
        rechargeRecordDO.setCreateTime(System.currentTimeMillis()/1000);
        rechargeRecordDO.setUpdateTime(System.currentTimeMillis()/1000);
        rechargeRecordMapper.saveRechargeRecord(rechargeRecordDO);
        String careObjectId = rechargeRecordDO.getCareObjectId();
        BigDecimal rechargeAmount = rechargeRecordDO.getRechargeAmount();
        rechargeRecordMapper.updateCareObjectCashBalance(rechargeAmount,careObjectId);

        HashMap<String , Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("code","200");
        return objectObjectHashMap;
    }


}
